package edu.sdibt.student.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.sdibt.DBUtil.DBConnection;
import edu.sdibt.bean.Student;
import edu.sdibt.bean.Student1;
import edu.sdibt.student.dao.StudentDAO;
import oracle.net.aso.p;

public class StudentDAOImpl implements StudentDAO{

	@Override
	public List<Student> StudentSelectAll() {
		// TODO Auto-generated method stub
		List<Student> list=new ArrayList<>();
		String sql="select * from user1";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			ResultSet rs=pstm.executeQuery();
			while(rs.next()) {
				Student student=new Student(rs.getString(1), rs.getString(2), rs.getString(3));
				list.add(student);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Student> StuSelectByMoHu(Student1 student1) {
		// TODO Auto-generated method stub
		List<Student> list=new ArrayList<>();
		String sql="select * from user1 where id like ? and user_name like ? "
				+ "and user_password like ? order by id";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(1, student1.getId());
			pstm.setString(2, student1.getName());
			pstm.setString(3, student1.getPassword());
			ResultSet rs=pstm.executeQuery();
			while(rs.next()){
				Student student2=new Student(rs.getString(1), rs.getString(2), rs.getString(3));
				list.add(student2);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public void StuDelById(String id) {
		// TODO Auto-generated method stub
		String sql="delete from user1 where id=?";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(1, id);
			pstm.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public int AddStu(Student student) {
		int row=0;
		String sql="insert into user1(id,user_name,user_password) values(?,?,?)";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(1, student.getId());
			pstm.setString(2, student.getName());
			pstm.setString(3, student.getPassword());
			row=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public int CheckStuName(String name){
		int row=0;
		String sql="select * from user1 where user_name=?";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(1, name);
			row=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public int update(Student student) {
		int row=0;
		 String sql="update user1 set user_name=?,user_password=? where id=?";
		 Connection connection=DBConnection.getConnection();
		 try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(3, student.getId());
			pstm.setString(1, student.getName());
			pstm.setString(2, student.getPassword());
			row=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public Student selectById(String id) {
		// TODO Auto-generated method stub
		Student student = null;
		String sql="select * from user1 where id=?";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement pstm=connection.prepareStatement(sql);
			pstm.setString(1, id);
			ResultSet rs=pstm.executeQuery();
			while(rs.next()) {
				student=new Student(rs.getString(1), rs.getString(2), rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student;
	}

	
}
